package org.genntii.warehousesystem.security;

import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.genntii.warehousesystem.domain.entity.Employee;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Data
@Builder
@NoArgsConstructor
public class EmployeeSecurity implements UserDetails {

    private Employee employee;

    public EmployeeSecurity(Employee employee){
        this.employee = employee;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        if (employee.getUsername().equals("genntii")){
            authorities.add(new SimpleGrantedAuthority("ROLE_SUPER_ADMIN"));
        }
        authorities.add(new SimpleGrantedAuthority("ROLE_EMPLOYEE"));
        return authorities;
    }

    @Override
    public String getPassword() {
        return employee.getPassword();
    }

    @Override
    public String getUsername() {
        return employee.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return employee.getStatus() == 1;
    }
}
